<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * Browse controller class
 *
 * Listing achievements
 *
 * @package		Achieve
 * @subpackage	Controllers
 * @category	Controllers
 * @author		Russ
 */
class Browse extends LoggedIn {

    /**
     * Serves the achievements page
     *
     * @access	public
     */
    public function index($categoryURI = '', $sort_by = 'category_id', $sort_order = 'desc') {

        $this->load->helper(array('url'));
        $this->load->model(array('Achievements_model'));
        $this->load->model(array('Category_model'));
        $userData = $this->session->all_userdata();
        $this->data['title'] = "List of Achievements";
        $this->data['allCategories'] = $this->Category_model->get_categories();

        $this->data['uri'] = $categoryURI;
        $this->data['sort_by'] = $sort_by;
        $this->data['sort_order'] = $sort_order;
        if( !(($this->data['sort_by'] == 'category_id' || $this->data['sort_by'] == 'points'
                || $this->data['sort_by'] == 'title' || $this->data['sort_by'] == 'date_created' )
                && ($this->data['sort_order'] == 'desc' || $this->data['sort_order'] == 'asc'))) {
            redirect('/browse/all');
            return;
        }

        if ($categoryURI != 'all'  ) {
                
            $categoryID = $this->Category_model->get_category_id($categoryURI);
            if ($categoryID == NULL) {
                redirect('/browse/all');
                return;
            }
        }
        if ($search_query = $this->input->get('searchquery')) {
            $this->data['achievements'] = $this->Achievements_model->get_available_achievements_search($userData['id'], $search_query, $this->data['sort_by'], $this->data['sort_order']);
        } elseif ($categoryURI != 'all') {
            $this->data['achievements'] = $this->Achievements_model->get_available_achievements_category($userData['id'], $categoryID, $this->data['sort_by'], $this->data['sort_order']);
        } else {
            $this->data['achievements'] = $this->Achievements_model->get_available_achievements($userData['id'], $this->data['sort_by'], $this->data['sort_order']);
        }

        $this->parser->parse("browse/browse.tpl", $this->data);
    }

}

?>